﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Data;

/// <summary>
/// Summary description for ShoppingCartBUS
/// </summary>
public class ShoppingCartBUS
{
    BouquetBUS bouBUS = new BouquetBUS();
    ArrayList gh;
    float amount = 0;

    public void Add(string B_ID, int number)
    {
        DataTable tb = new DataTable();
        tb = bouBUS.returnAllBouquet();
        DataView view = new DataView(tb);
        view.RowFilter = "B_ID ='" + B_ID + "'";
        if (HttpContext.Current.Session["GioHang"] == null)
        {
            HttpContext.Current.Session["GioHang"] = new ArrayList();
        }
        gh = (ArrayList)HttpContext.Current.Session["GioHang"];
        foreach (ShoppingCart sp in gh)
        {
            if (sp.Id.Equals(view[0]["B_ID"].ToString()))
            {
                int slNew = sp.Number + number;
                ShoppingCart h = new ShoppingCart(sp.Id, sp.Image, sp.Name, slNew, sp.Price);
                gh.Remove(sp);
                gh.Add(h);
                return;
            }
        }

        ShoppingCart h1 = new ShoppingCart(view[0]["B_ID"].ToString(), view[0]["B_Image"].ToString(), view[0]["B_Name"].ToString(), number, float.Parse(view[0]["B_Price"].ToString()));
        gh.Add(h1);
        HttpContext.Current.Session["GioHang"] = gh;
        HttpContext.Current.Session["SLItem"] = gh.Count;
    }

    public void Update(string B_ID, int Number)
    {
        if (Number == 0)
        {
            Del(B_ID);
            return;
        }
        gh = (ArrayList)HttpContext.Current.Session["GioHang"];
        foreach (ShoppingCart sp in gh)
        {
            if (sp.Id.Equals(B_ID))
            {

                ShoppingCart h = new ShoppingCart(sp.Id, sp.Image, sp.Name, Number, sp.Price);
                gh.Remove(sp);
                gh.Add(h);
                return;
            }
        }
        HttpContext.Current.Session["GioHang"] = gh;
        HttpContext.Current.Session["SLItem"] = gh.Count;

    }

    public void Del(string B_ID)
    {
        gh = (ArrayList)HttpContext.Current.Session["GioHang"];
        foreach (ShoppingCart sp in gh)
        {
            if (sp.Id.Equals(B_ID))
            {
                gh.Remove(sp);
                HttpContext.Current.Session["GioHang"] = gh;
                HttpContext.Current.Session["SLItem"] = gh.Count;
                return;
            }
        }
    }

    public void TotalAmount()
    {

        if (HttpContext.Current.Session["GioHang"] == null)
        {
            HttpContext.Current.Session["GioHang"] = new ArrayList();
            HttpContext.Current.Session["TotalAmount"] = 0;
        }
        gh = (ArrayList)HttpContext.Current.Session["GioHang"];
        foreach (ShoppingCart sp in gh)
        {
            amount = amount + sp.Amount;
        }
        HttpContext.Current.Session["TotalAmount"] = amount;
    }
}